package com.haoshui.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haoshui.constants.SystemConstants;
import com.haoshui.domain.ResponseResult;
import com.haoshui.domain.entity.Comment;
import com.haoshui.domain.entity.User;
import com.haoshui.domain.vo.CommentChildVo;
import com.haoshui.domain.vo.CommentListVo;
import com.haoshui.domain.vo.PageVo;
import com.haoshui.enums.AppHttpCodeEnum;
import com.haoshui.service.CommentService;
import com.haoshui.mapper.CommentMapper;
import com.haoshui.service.UserService;
import com.haoshui.utils.BeanCopyUitls;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.function.Consumer;

/**
 * 评论表(Comment)表服务实现类
 *
 * @author makejava
 * @since 2022-09-28 10:47:56
 */
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private UserService userService;


    @Override
    public ResponseResult commentList(String s, Long articleId, Integer pageNum, Integer pageSize) {
        //查找一级评论
        Page<Comment> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        //是一级评论
        queryWrapper.eq(Comment::getRootId, SystemConstants.COMMENT_ROOT_NOONE);
        //是此文章的评论
        queryWrapper.eq(SystemConstants.COMMENT_TYPE_ARTICLE.equals(s), Comment::getArticleId, articleId);
        queryWrapper.eq(Comment::getType, s);

        page(page, queryWrapper);

        //获取一级评论的二级评论
        List<Comment> records = page.getRecords();
        //封装Vo
        List<CommentListVo> commentListVos = BeanCopyUitls.copyBeanList(records, CommentListVo.class);
        //获取commentListVos的userName
        commentListVos.stream().forEach(new Consumer<CommentListVo>() {
            @Override
            public void accept(CommentListVo commentListVo) {
                Long createBy = commentListVo.getCreateBy();
                User byId = userService.getById(createBy);

                if(byId != null){
                    String nickName =  byId.getNickName();
                    commentListVo.setUsername(nickName);
                }else{
                    commentListVo.setUsername("用户已消失");
                }

            }
        });

        //对每一个一级评论，查找他的二级评论
        for (CommentListVo firstComment : commentListVos) {
            //每一个一级评论查找二级评论
            queryWrapper = new LambdaQueryWrapper<>();
            //是评论一级评论的评论
            queryWrapper.eq(Comment::getRootId, firstComment.getId());
            //是评论此文章的评论
            queryWrapper.eq(Comment::getArticleId, articleId);
            //升序排
            queryWrapper.orderByAsc(Comment::getCreateTime);
            List<Comment> secondComment = list(queryWrapper);


            //封装子评论
            List<CommentChildVo> commentChildVos = BeanCopyUitls.copyBeanList(secondComment, CommentChildVo.class);
            commentChildVos.stream().forEach(new Consumer<CommentChildVo>() {
                @Override
                public void accept(CommentChildVo commentChildVo) {
                    Long createBy = commentChildVo.getCreateBy();
                    User byId = userService.getById(createBy);

                    //用户被删除
                    if(byId != null){
                        String nickName =  byId.getNickName();
                        commentChildVo.setUsername(nickName);
                    }else{
                        commentChildVo.setUsername("用户已消失");
                    }

                    User byId1 = userService.getById(commentChildVo.getToCommentUserId());

                    //用户被删除
                    if(byId != null){
                        String nickName =  byId1.getNickName();
                        commentChildVo.setToCommentUserName(nickName);
                    }else{
                        commentChildVo.setToCommentUserName("用户已消失");
                    }
                }
            });
            firstComment.setChildren(commentChildVos);
        }

        return ResponseResult.okResult(new PageVo(commentListVos, page.getTotal()));
    }


    @Override
    public ResponseResult addComment(Comment comment) {
        //如果评论是空,返回错误
        if(!StringUtils.hasText(comment.getContent())){
            return ResponseResult.errorResult(AppHttpCodeEnum.COMMENT_NOT_NOLL);
        }

        //插入评论
        save(comment);
        return ResponseResult.okResult();
    }


}
